package com.moco.launcher.activity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;

import com.moco.launcher.R;
import com.moco.launcher.bean.MOCO_CONSTANTS;
import com.moco.launcher.link.LinkManager;
import com.moco.launcher.play.AndroidMusicPlayer;
import com.moco.launcher.view.ShutDownDialog;
import com.oazon.common.util.RequestManager;
import com.oazon.common.util.Utils;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LinkEventSource;

public class AlarmClockActivity extends BaseFragmentActivity   {
	private static final String TAG = "alarmClockActivity";
    ViewPager viewPager ;
	Context mContext;
	LinearLayout alarm_clock_linear;
	Bitmap ab;
	ArrayList<View> viewList;
	ArrayList<View> viewContainter = new ArrayList<View>();
	//GifView gf;
	//ImageView forTakeVideoAudio;
	
	public int TO_START_GIFVIEW=0x1;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext= this;
		
		setContentView(R.layout.alarm_clock_activity);
		//forTakeVideoAudio = (ImageView) findViewById(R.id.for_take_video_audio);
/*		forTakeVideoAudio.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AlarmClockActivity.this.finish();
			}
		});*/
	//	handler.sendEmptyMessageDelayed(TO_START_GIFVIEW, 15000);
		
	//	viewPager = (ViewPager) findViewById(R.id.view_pager);

	//	alarm_clock_linear = (LinearLayout) findViewById(R.id.alarm_clock_linear);
		
		//aa.start();
		
		
	}
	public void initGif(){/*
		gf = (GifView) findViewById(R.id.gif);
		gf.setGifImage(R.drawable.b);
		gf.setShowDimension(320, 320);
		gf.setGifImageType(GifImageType.COVER);
		gf.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				gf.destroyDrawingCache();
			//	gf.d
				
				gf.setVisibility(View.GONE);
				//gf.destroyDrawingCache();
				//gf.destroyDrawingCache();
				forTakeVideoAudio.setVisibility(View.VISIBLE);
				
				//AlarmClockActivity.this.finish();
				return false;
			}
		});

		
	*/}
	
	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0x1:
				initGif();
				break;
			default:
				break;
			}
		};
	};
//	以下代码虽然未经启用，但是作为预留功能，请勿删除-vincent.tsai
//  must not del  the code here.even though there do not work  -vincent.tsai
	
/*	Thread aa = new Thread(new Runnable() {
		
		@Override
		public void run() {
			// TODO Auto-generated method stub
		int pngCount = getToOpenUrl("http://update.o2fun.com/file/demo/picinfo.txt");
		if (pngCount!=999999999){
			for (int i= 1;i<=pngCount;i++){
				requesImage("http://update.o2fun.com/file/demo/"+i+".png",i);
				Log.e(TAG, "http://update.o2fun.com/file/demo/"+i+".png");
			}
		}
		}
	});
	private void requesImage(String imageUrl ,int i) {

			RequestQueue queue = RequestManager.getRequestQueue();
			
			ImageRequest imagerequest = new ImageRequest(imageUrl,
					new Response.Listener<Bitmap>() {

						@Override
						public void onResponse(Bitmap arg0) {
							if(aboutview == null){
								return;
							}
							if(arg0 == null){
								//aboutview.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.default_null_icon) );
							}else{
								ab=arg0;
								handler.removeMessages(0);
								Message msg = handler.obtainMessage(0);
								handler.sendMessage(msg);
								//aboutview.setImageBitmap(arg0);
							}
						}
					}, 0, 0, Config.RGB_565, new Response.ErrorListener() {

						@Override
						public void onErrorResponse(VolleyError arg0) {
//							aboutview.setImageResource(R.drawable.ic_launcher);
						}
					});
			queue.add(imagerequest);
		

		
	}

	


	Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			ImageView aa = new ImageView(mContext);

			aa.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
			aa.setImageBitmap(ab);
			aa.setOnLongClickListener(new OnLongClickListener() {
				
				@Override
				public boolean onLongClick(View v) {
					// TODO Auto-generated method stub
					Log.e(TAG, "imageview is longclick");
					AlarmClockActivity.this.finish();
					return false;
				}
			});
			//alarm_clock_linear.addView(aa);
			viewContainter.add(aa);
			viewPager.setAdapter(new PagerAdapter() {
				 
	            //viewpager中的组件数量
	            @Override
	            public int getCount() {
	                return viewContainter.size();
	            }
	          //滑动切换的时候销毁当前的组件
	            @Override
	            public void destroyItem(ViewGroup container, int position,
	                    Object object) {
	                ((ViewPager) container).removeView(viewContainter.get(position));
	            }
	          //每次滑动的时候生成的组件
	            @Override
	            public Object instantiateItem(ViewGroup container, int position) {
	                ((ViewPager) container).addView(viewContainter.get(position));
	                return viewContainter.get(position);
	            }
	 
	            @Override
	            public boolean isViewFromObject(View arg0, Object arg1) {
	                return arg0 == arg1;
	            }
	 
	            @Override
	            public int getItemPosition(Object object) {
	                return super.getItemPosition(object);
	            }
	 
	        
	        });
			viewPager.setOnPageChangeListener(new OnPageChangeListener() {
	            @Override
	            public void onPageScrollStateChanged(int arg0) {
	                Log.d(TAG, "--------changed:" + arg0);
	            }
	 
	            @Override
	            public void onPageScrolled(int arg0, float arg1, int arg2) {
	                Log.d(TAG, "-------scrolled arg0:" + arg0);
	                Log.d(TAG, "-------scrolled arg1:" + arg1);
	                Log.d(TAG, "-------scrolled arg2:" + arg2);
	            }
	 
	            @Override
	            public void onPageSelected(int arg0) {
	                Log.d(TAG, "------selected:" + arg0);
	            }
	        });
	 
	    
		    Log.e(TAG, ab+"");
		    Log.e(TAG, aa+"");
		}
	};
	
	
	private int getToOpenUrl(String urladdr) {
		String resultData = "";
		try {
			
			URL url = new URL(urladdr);
			if (url != null) {
				HttpURLConnection connection = (HttpURLConnection) url
						.openConnection();
				connection.setConnectTimeout(1000 * 5);
				InputStreamReader in = new InputStreamReader(
						connection.getInputStream());
				BufferedReader buffer = new BufferedReader(in);
				String inputLine = null;
				while ((inputLine = buffer.readLine()) != null) {
					resultData += inputLine ;
				}
				in.close();
				buffer.close();
				connection.disconnect();
				Log.e(TAG, resultData);
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		int i; 
        if(resultData.equals("")){
		        i=999999999;
				}else{
					i= Integer.parseInt(resultData);
				}
		return i;
	
	}*/


  /*  viewPager.setAdapter(pagerAdapter);*/
	
}
